﻿/**
* 向编辑器插入内容
* start: 选择的文字前面插入的字符串
* end: 选择的文字后面插入的字符串，可选则插入空字符串
*/
function insertEditor(start, end) {
    if (!end) {
        end = '';
    }
    var selection = editor.getSelection();
    if (selection.length == 0) {
        start = '\r' + start;
    }
    var text = start + selection + end;
    editor.replaceSelection(text)
}


/**
       * 插入代码
       */
function insertCode(value) {
    insertEditor('```' + value + '\n这里输入你的代码\n```\n')
}

/**
 * 插入超链接
 */
function insertLink() {
    bootbox.prompt({
        title: '填写 url 超链接',
        closeButton: false,
        buttons: {
            cancel: {
                label: '关闭'
            },
            confirm: {
                label: '插入链接'
            }
        },
        callback: function (result) {
            if (result) {
                insertEditor('[' + result + '](' + result + ') ');
            }
        }
    })
}

function insertImageLink(link,text) {
    insertEditor('\n\n![' + text+'](' + link + ') ','\n\n');
}

function mdExampleModal() {
    bootbox.alert({
        title: 'Markdown 语法帮助',
        closeButton: false,
        message: '<ul class="list-group">'
            + '<li class="list-group-item"><h1># 标题1</h1></li>'
            + '<li class="list-group-item"><h2>## 标题2</h2></li>'
            + '<li class="list-group-item"><h3>### 标题3</h3></li>'
            + '<li class="list-group-item"><h4>#### 标题4</h4></li>'
            + '<li class="list-group-item"><h5>##### 标题5</h5></li>'
            + '<li class="list-group-item"><h6>###### 标题6</h6></li>'
            + '<li class="list-group-item"><b>**粗体**</b></li>'
            + '<li class="list-group-item"><i>*斜体*</i></li>'
            + '<li class="list-group-item">* 无序列表</li>'
            + '<li class="list-group-item"><blockquote> 插入引用</blockquote></li>'
            + '<li class="list-group-item"><code>```代码```</code></li>'
            + '<li class="list-group-item">[超链接](url)</li>'
            + '<li class="list-group-item">![图片](url)</li>'
            +
            '</ul> '
    })
}